//
//  CTRegisterViewController.m
//  ConstellationTalk_0.1
//
//  Created by riley on 15/4/20.
//  Copyright (c) 2015年 riley chu. All rights reserved.
//

#import "CTRegisterViewController.h"
#import "CTPickImageViewController.h"

//view
#import "CTRegisterView.h"

//logic
#import "CTRegisterLogic.h"

//model
#import "CTLoginItems.h"

//header
#import "CTHeader.h"
//function
#import "CTDebugManager.h"
#import "CTTextManager.h"
#import "CTRemindView.h"
#import "CTAppLanguage.h"
#import "CTActivitorView.h"
#import "CTDeviceFunction.h"
#import "CTReachability.h"
#import "CTGenRequestData.h"
#import "MobClick.h"

//tool
#import "CRSA.h"

#import "CTRestPasswordView.h"
@interface CTRegisterViewController ()<CTRegisterViewDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>
{
    CTRegisterView * registerView;
    CTRegisterLogic * registerLogic;
    CTActivitorView * acView;
    
    UIImagePickerController * albumPickerVC;
    UIImage * headImage;
    
    NSString * userName;
    NSString * passwordStr;
    NSString * resetPasswordStr;
    NSString * genderStr;
}

@end


@implementation CTRegisterViewController
@synthesize phoneNumber;


- (void)dealloc
{
    [registerView removeFromSuperview];
    registerView = nil;
    registerLogic = nil;
    albumPickerVC = nil;
    albumPickerVC.delegate = nil;
    headImage = nil;
    
    [acView removeFromSuperview];
    acView = nil;
    
    phoneNumber = nil;
    userName = nil;
    passwordStr = nil;
    resetPasswordStr = nil;
    genderStr = nil;
    [[NSNotificationCenter defaultCenter] removeObserver:self name:CT_NOTI_SET_HEAD object:nil];

    [CTDebugManager showDebugLogInfo:@"dealloc : CTRegisterViewController Is Removed!!!"];
}


- (instancetype)init
{
    self = [super init];
    if (self) {
        
        genderStr = @"男";
        
        registerLogic = [[CTRegisterLogic alloc] init];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(setPersonalHead:) name:CT_NOTI_SET_HEAD object:nil];

    }
    return self;
}
-(void)viewDidAppear:(BOOL)animated
{
    //统计
    [MobClick beginLogPageView:@"CTRegisterViewController"];
    [super viewDidAppear:animated];
}

-(void)viewWillDisappear:(BOOL)animated
{
    //统计
    [MobClick endLogPageView:@"CTRegisterViewController"];
    [super viewWillDisappear:animated];
}


-(void)viewDidLoad
{
    [super viewDidLoad];
    
    registerView = [[CTRegisterView alloc] init];
    registerView.delegate = self;
    [self.view addSubview:registerView];
    

    
    
    
    //初始化相册
    albumPickerVC =  [[UIImagePickerController alloc] init];
    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
        albumPickerVC.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        albumPickerVC.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:albumPickerVC.sourceType];
        
    }
    albumPickerVC.delegate = self;
    albumPickerVC.allowsEditing = NO;
    
}

-(void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    [CTDebugManager showDebugLogInfo:@"CTRegisterViewController内存警告!!!"];
}


#pragma mark - register view delegate
-(void) registerViewUserTextFieldText:(NSString *)text  registerView:(CTRegisterView *)registerView
{
    userName = text;
}
-(void) registerViewPassTextFieldText:(NSString *)text  registerView:(CTRegisterView *) registerView
{
    passwordStr = text;
}
-(void) registerViewPassWordTextFieldText:(NSString *)text  registerView:(CTRegisterView *) registerView
{
    resetPasswordStr = text;
}
-(void)registerViewsexBtnText:(NSString *)text registerView:(CTRegisterView *)registerView
{
    
    @try {
        if ([[CTTextManager shareInstance] isBlankString:text] == NO) {
            if ([text isEqualToString:@"男"]) {
                genderStr = @"男";
            }else if ([text isEqualToString:@"女"])
            {
                genderStr = @"女";
            }
        }
    }
    @catch (NSException *exception) {
        
    }
   
    
}
//更换头像
-(void) registerViewHeadButton:(CTRegisterView *) registerView
{
    CTPickImageViewController * pickImageVC = [[CTPickImageViewController alloc] init];
    pickImageVC.type = CTPickImageViewControllerTypeSetHead;
    UINavigationController * pickImageNav = [[UINavigationController alloc] initWithRootViewController:pickImageVC];
    
    pickImageNav.navigationBarHidden = YES;
    [self presentViewController:pickImageNav animated:YES completion:^{
        
    }];

}
//返回按钮
-(void) registerViewBackButton:(CTRegisterView *)registerView
{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void) registerViewRegisterButton:(CTRegisterView *)registerView
{
    if (headImage == nil) {
        CTRemindView * remindView = [[CTRemindView alloc] init];
        [remindView showRemindViewOn:self.view withRemindWord:[CTAppLanguage localizedStringForKey:@"remindView_login_q" withValue:nil withTable:@"Language"]];
        return;
    }
    
    if ([[CTTextManager shareInstance] isBlankString:userName] == YES) {
        CTRemindView * remindView = [[CTRemindView alloc] init];
        [remindView showRemindViewOn:self.view withRemindWord:[CTAppLanguage localizedStringForKey:@"remindView_login_s" withValue:nil withTable:@"Language"]];
        return;
    }
    
    if ([[CTTextManager shareInstance] charNumber:userName] > 14) {
        CTRemindView * remindView = [[CTRemindView alloc] init];
        [remindView showRemindViewOn:self.view withRemindWord:[CTAppLanguage localizedStringForKey:@"remindView_login_r" withValue:nil withTable:@"Language"]];
        return;
    }
    
    if ([[CTTextManager shareInstance] isFoundString:@" " withStr:userName]) {
        CTRemindView * remindView = [[CTRemindView alloc] init];
        [remindView showRemindViewOn:self.view withRemindWord:[CTAppLanguage localizedStringForKey:@"remindView_login_t" withValue:nil withTable:@"Language"]];
        return;
    }
    
    
    if ([[CTTextManager shareInstance] isBlankString:passwordStr] == YES ) {
        CTRemindView * remindView = [[CTRemindView alloc] init];
        [remindView showRemindViewOn:self.view withRemindWord:[CTAppLanguage localizedStringForKey:@"remindView_login_d" withValue:nil withTable:@"Language"]];
        return;
    }
    if ([[CTTextManager shareInstance] isBlankString:resetPasswordStr] == YES ) {
        CTRemindView * remindView = [[CTRemindView alloc] init];
        [remindView showRemindViewOn:self.view withRemindWord:[CTAppLanguage localizedStringForKey:@"remindView_login_d" withValue:nil withTable:@"Language"]];
        return;
    }
    
    if (![passwordStr isEqualToString:resetPasswordStr]) {
        CTRemindView * remindView = [[CTRemindView alloc] init];
        [remindView showRemindViewOn:self.view withRemindWord:[CTAppLanguage localizedStringForKey:@"remindView_login_k" withValue:nil withTable:@"Language"]];
        return;
    }
    
    if (passwordStr.length < 6 || passwordStr.length > 14) {
        CTRemindView * remindView = [[CTRemindView alloc] init];
        [remindView showRemindViewOn:self.view withRemindWord:[CTAppLanguage localizedStringForKey:@"logInView_PasswordDescription_a" withValue:nil withTable:@"Language"]];
        return;
    }
    if ([[CTTextManager shareInstance] isMatch:passwordStr] == NO) {
        CTRemindView * remindView = [[CTRemindView alloc] init];
        [remindView showRemindViewOn:self.view withRemindWord:[CTAppLanguage localizedStringForKey:@"logInView_PasswordDescription_b" withValue:nil withTable:@"Language"]];
        
        return;
    }
    
    
    //监测网络
    if ([self checkNetworkState] == NO)return;
    
    [self showRemindView];

    
    //注册
    
    [registerLogic mobileRegisterRequest:[CTGenRequestData genMobileRegisterRequestItemUserName:userName withPassword:passwordStr withPhoneNumber:phoneNumber withGender:genderStr withHeadImg:headImage]  successCompletionHandler:^{
        dispatch_async(dispatch_get_main_queue(), ^{
            [acView removeActivitorView];
            CTRemindView * remindView = [[CTRemindView alloc] init];
            [remindView showRemindViewOn:self.view withRemindWord:[CTAppLanguage localizedStringForKey:@"remindView_login_u" withValue:nil withTable:@"Language"]];
            [self.navigationController dismissViewControllerAnimated:YES completion:^{
                
            }];
        });
    } failedHandler:^{
        dispatch_async(dispatch_get_main_queue(), ^{
            [acView removeActivitorView];
            CTRemindView * remindView = [[CTRemindView alloc] init];
            [remindView showRemindViewOn:self.view withRemindWord:[CTAppLanguage localizedStringForKey:@"remindView_login_v" withValue:nil withTable:@"Language"]];
        });
    }];
    
    
}





#pragma mark - view on main thread -


-(void)showRemindView
{
    acView = [[CTActivitorView alloc] init];
    [acView showActivitorViewOn:self.view];
}




#pragma mark - check net work state -
-(BOOL)checkNetworkState
{
    NSString * descri;
    CTRemindView * remindView;
    
    CTNetWorkReachabilityStatus  status = [CTReachability judegeCurrentNetWorkStatus];
    switch (status) {
        case CTNetWorkReachabilityStatusUnknown:
            descri = [CTAppLanguage localizedStringForKey:@"remindView_login_networkUnknow" withValue:nil withTable:@"Language"];
            remindView = [[CTRemindView alloc] init];
            [remindView showRemindViewOn:self.view withRemindWord:descri];
            return NO;
            break;
        case CTNetWorkReachabilityStatusNotReachable:
            descri = [CTAppLanguage localizedStringForKey:@"remindView_login_networkReachable" withValue:nil withTable:@"Language"];
            remindView = [[CTRemindView alloc] init];
            [remindView showRemindViewOn:self.view withRemindWord:descri];
            return NO;
            break;
        default:
            break;
    }
    return YES;
}


#pragma mark - UIImagePicker Delagete

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo
{
    headImage = image;
    registerView.image = image;
    
    [picker dismissViewControllerAnimated:YES completion:^{
        
    }];
}

#pragma mark - noti -

-(void)setPersonalHead:(id)sender
{
    NSNotification * noti = (NSNotification *)sender;
    UIImage * headImg = (UIImage *)noti.object;
    
    registerView.image = headImg;
    headImage = headImg;
}

@end
